package org.jeecg.modules.demo.com.kunpeng.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.com.kunpeng.entity.CommodityPrice;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.CommodityPriceQueryListDTO;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.CommodityPurchaseOrSaleDTO;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceCode;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceException;
import org.jeecg.modules.demo.com.kunpeng.mapper.CommodityPriceMapper;
import org.jeecg.modules.demo.com.kunpeng.service.ICommodityPriceService;
import org.jeecg.modules.demo.com.kunpeng.vo.CommodityPriceQueryListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: 商品价目表
 * @Author: jeecg-boot
 * @Date:   2023-01-11
 * @Version: V1.0
 */
@Service
public class CommodityPriceServiceImpl extends ServiceImpl<CommodityPriceMapper, CommodityPrice> implements ICommodityPriceService {



    @Autowired
    private CommodityPriceMapper commodityPriceMapper;

    private static Integer KEY;
    /**
     * 新增
     * @param commodityPrice
     */
    @Override
    public void insert(CommodityPrice commodityPrice) {
        List<CommodityPrice> commodityPrices = commodityPriceMapper.queryListByTradeNameOrSpecificationOrSupplierOrBrand(commodityPrice.getTradeName(), commodityPrice.getSpecification(),
                                                    commodityPrice.getSupplier(), commodityPrice.getCommodityBrand());
        if(commodityPrices.size()>0){
            String message="该商品已存在，请重新填写！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        CommodityPriceQueryListVO commodityPriceQueryListVO = commodityPriceMapper.selectCommodityByCommodityCode(commodityPrice.getCommodityCode());
        if(null != commodityPriceQueryListVO){
            String message="商品编码重复，请重新填写！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        commodityPrice.setIsDelete("0");
        commodityPriceMapper.insert(commodityPrice);
    }

    /**
     * 模糊查询
     * @param page
     * @param commodityPriceQueryListDTO
     * @return
     */
    @Override
    public Page<CommodityPriceQueryListVO> queryListByKeyWords(Page<CommodityPriceQueryListVO> page, CommodityPriceQueryListDTO commodityPriceQueryListDTO) {
        Page<CommodityPriceQueryListVO> commodityPriceQueryListVOPage = page.setRecords(commodityPriceMapper.queryListByKeyWords(page, commodityPriceQueryListDTO.getCommodityClassification(),
                                                                            commodityPriceQueryListDTO.getSupplier(), commodityPriceQueryListDTO.getKeyWords()));
        int key=1;
        for (CommodityPriceQueryListVO commodityPriceQueryListVO:commodityPriceQueryListVOPage.getRecords()){
            commodityPriceQueryListVO.setKey(key++);
            //获取商品添加时间
            Date createTime = commodityPriceQueryListVO.getCreateTime();
            //
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.setTime(createTime);
            //判断供货单位
            if(commodityPriceQueryListVO.getCompany().equals("年")){
                String supplyCycle = commodityPriceQueryListVO.getSupplyCycle();
                int num = Integer.parseInt(supplyCycle);
                cal.add(Calendar.YEAR, num);
            }else if(commodityPriceQueryListVO.getCompany().equals("月")){
                String supplyCycle = commodityPriceQueryListVO.getSupplyCycle();
                int num = Integer.parseInt(supplyCycle);
                cal.add(Calendar.MONTH, num);
            }else if(commodityPriceQueryListVO.getCompany().equals("日")){
                String supplyCycle = commodityPriceQueryListVO.getSupplyCycle();
                int num = Integer.parseInt(supplyCycle);
                cal.add(Calendar.DATE, num);
            }
            //计算完的供货时间赋值到对象中
            String planSupplyDate = simpleDateFormat.format(cal.getTime());
            commodityPriceQueryListVO.setPlanSupplyDate(planSupplyDate);
        }
        return commodityPriceQueryListVOPage;
    }

    /**
     * 修改isDelete状态
     * @param id
     * @param isDelete
     */
    @Override
    public void updateIsDelete(String id, String isDelete) {
        commodityPriceMapper.updateIsDelete(id,isDelete);
    }


    /**
     * 根据商品编码查询商品详情
     * @param commodityCode
     * @return
     */
    @Override
    public CommodityPriceQueryListVO selectCommodityByCommodityCode(String commodityCode) {
        CommodityPriceQueryListVO commodityPriceQueryListVO = commodityPriceMapper.selectCommodityByCommodityCode(commodityCode);
        return commodityPriceQueryListVO;
    }

    /**
     * 采购商品列表
     */
    @Override
    public Page<CommodityPriceQueryListVO> queryPurchaseCommodityList(Page<CommodityPriceQueryListVO> page, CommodityPurchaseOrSaleDTO commodityPurchaseOrSaleDTO) {
        List<CommodityPriceQueryListVO> commodityPriceQueryListVOS = commodityPriceMapper.queryPurchaseCommodityList(page, commodityPurchaseOrSaleDTO.getKeyWords());
        if(commodityPriceQueryListVOS.size()>0){
            KEY=1;
            for(CommodityPriceQueryListVO commodityPriceQueryListVO:commodityPriceQueryListVOS){
                commodityPriceQueryListVO.setKey(KEY++);
            }
        }
        Page<CommodityPriceQueryListVO> commodityPriceQueryListVOPage = page.setRecords(commodityPriceQueryListVOS);
        return commodityPriceQueryListVOPage;
    }

    /**
     * 销售商品列表
     */
    @Override
    public Page<CommodityPriceQueryListVO> querySalesCommodityList(Page<CommodityPriceQueryListVO> page, CommodityPurchaseOrSaleDTO commodityPurchaseOrSaleDTO) {
        List<CommodityPriceQueryListVO> commodityPriceQueryListVOS = commodityPriceMapper.querySalesCommodityList(page, commodityPurchaseOrSaleDTO.getKeyWords());
        if(commodityPriceQueryListVOS.size()>0){
            KEY=1;
            for (CommodityPriceQueryListVO commodityPriceQueryListVO:commodityPriceQueryListVOS){
                commodityPriceQueryListVO.setKey(KEY++);
            }
        }
        Page<CommodityPriceQueryListVO> commodityPriceQueryListVOPage = page.setRecords(commodityPriceQueryListVOS);
        return commodityPriceQueryListVOPage;
    }


    /**
     * 生成商品编码
     * @param commodityCode
     * @return
     */
    @Override
    public Integer commodityPriceListByCode(String commodityCode) {
        List<CommodityPrice> commodityPrices = commodityPriceMapper.commodityPriceListByCode(commodityCode);
        Integer cCode;
        if(commodityPrices.size()>0){
            CommodityPrice commodityPrice = commodityPrices.get(commodityPrices.size() - 1);
            String code = commodityPrice.getCommodityCode().substring(10,13);
            System.out.println("code:"+code);
            cCode = Integer.parseInt(code)+1;
        }else {
            cCode = 1;
        }
        return cCode;
    }

}
